/******************************************************************************
 * This file is part of libemb.
 *
 * libemb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * libemb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libemb.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Project: Embedme
 * Author : FergusZeng
 * Email  : cblock@126.com
 * git	  : https://gitee.com/newgolo/embedme.git
 * Copyright 2014-2020 @ ShenZhen ,China
*******************************************************************************/
#ifndef __LINE_BUFFER_H__
#define __LINE_BUFFER_H__

#include "BaseType.h"
#include "ThreadUtil.h"
/**
 * @file LineBuffer.h
 * @brief 线性缓冲区
 */
namespace libemb{
/**
 * @class LineBuffer 
 * @brief 线性缓冲
 */
class LineBuffer{
public:
	LineBuffer();
	~LineBuffer();
	/**
	 * @brief 初始化线性缓冲区
	 * @param max 线性缓冲区长度
	 * @return true 执行成功
	 * @return false 执行失败
	 */
	bool init(int max);
	/**
	 * @brief 放置数据
	 * @param data 数据
	 * @param size 数据长度
	 * @return int 放入的数据长度
	 */
	int push(char*data, int size);
	/**
	 * @brief 取出数据
	 * @param buf 数据缓存
	 * @param size 数据长度int
	 * @return int 获取的数据长度
	 */
	int pop(char* buf, int size);
	/**
	 * @brief 在缓冲区中查找字符串
	 * @param str 字符串指针
	 * @param size 字符串长度
	 * @return int 返回字符串在缓冲区的起始位置，-1表示未找到
	 */
	int find(char* str,int size);
	/**
	 * @brief 重载[]符号，获取缓冲区字符
	 * @param idx 索引
	 * @return char idx位置处的字符
	 */
	char operator[](int idx);
	/**
	 * @brief 清空缓冲区
	 */
	void clear();
private:
	Mutex m_mutex;
	char* m_buffer;
    char* m_endpos;
    char* m_curpos;
    int m_size;
};
}
#endif

